//
//  HomeViewController.m
//  TheOne
//
//  Created by lanou3g on 16/3/11.
//  Copyright © 2016年 Wicky. All rights reserved.
//

#import "HomeViewController.h"
#import <CoreLocation/CoreLocation.h>
#import "ViewControllerTool.h"
#define WeatherPrefixUrl @"http://apistore.baidu.com/microservice/weather?cityid="
@interface HomeViewController ()<CLLocationManagerDelegate>
@property (weak, nonatomic) IBOutlet UIView *container;
@property (weak, nonatomic) IBOutlet UILabel *LbLikeCount;
@property (strong,nonatomic)CLLocationManager * CLManager;
@property (strong,nonatomic)CLGeocoder * coder;
@property (strong,nonatomic)NSDictionary * CodeForCity;
@property (strong,nonatomic)NSString * state;
@property (strong,nonatomic)NSString * city;
@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString * filePath = [[NSBundle mainBundle] pathForResource:@"CodeForCity" ofType:@"plist"];
    self.CodeForCity = [NSDictionary dictionaryWithContentsOfFile:filePath];
    [self setViews];
    [self getWeather];
}
-(void)setViews
{
    self.container.layer.cornerRadius = 5;
    self.container.layer.borderColor = HWColor(221, 220, 227).CGColor;
    self.container.layer.borderWidth = 2;
}
-(void)getWeather
{
    self.CLManager = [CLLocationManager new];
    if ([[[UIDevice currentDevice] systemVersion] floatValue] > 8.0) {
        if ([CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorizedWhenInUse) {
            [self.CLManager requestAlwaysAuthorization];
        }
    }
    self.CLManager.delegate = self;
    self.CLManager.desiredAccuracy = 100;
    self.CLManager.distanceFilter = 100;
    [self.CLManager startUpdatingLocation];
    self.coder = [CLGeocoder new];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
    NSLog(@"定位成功");
    [self deCode:locations.firstObject];
    [self.CLManager stopUpdatingLocation];
}
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@"定位失败");
}

-(void)deCode:(CLLocation *)location
{
    if (self.coder.isGeocoding) {
        return;
    }
    //    反编码为地理位置
    [self.coder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        if (error) {
            return ;
        }
        //        取出具体位置信息
        CLPlacemark * placeMark = placemarks.firstObject;
        //        坐标
        [placeMark.addressDictionary enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
            if ([key isEqualToString:@"State"]) {
                self.state = [obj substringToIndex:2];
            }
            if ([key isEqualToString:@"City"]) {
                self.city = [obj substringToIndex:2];
            }
        }];
        NSDictionary * StateArr = [self.CodeForCity valueForKey:self.state];
        NSString * code = [StateArr valueForKey:self.city][0][@"code"];
        NSLog(@"%@",code);
        [[ViewControllerTool new] getDataWithUrlStr:[NSString stringWithFormat:@"%@%@",WeatherPrefixUrl,code] SuccessBlock:^(NSDictionary *dic) {
            NSLog(@"%@",dic);
        }];
    }];
}
- (IBAction)DairyBtnAction:(id)sender {
    NSLog(@"1");
}
- (IBAction)LikeBtnAction:(id)sender {
    NSLog(@"2");
}
- (IBAction)MoreBtnAction:(id)sender {
    NSLog(@"3");
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
